package com.zf.shuashua.advert;

import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.ConfigAdvertHot;
import com.zf.shuashua.bean.ConfigAdvertOpen;
import com.zf.shuashua.bean.ConfigInfo;
import com.zf.shuashua.bean.advert.AdvertConfigList;
import com.zf.shuashua.bean.advert.CommonConfigBean;
import com.zf.shuashua.bean.advert.DrawConfigBean;
import com.zf.shuashua.bean.advert.PatchAccident;
import com.zf.shuashua.bean.advert.PatchConfigBean;
import com.zf.shuashua.bean.advert.ThirdAdOpen;

/**
 * 开关管理类
 */
public class AdvertConfigManager {

    private static AdvertConfigManager instance;

    private AdvertConfigManager() {
    }

    public static AdvertConfigManager getInstance() {
        if (null == instance) {
            synchronized (AdvertConfigManager.class) {
                if (null == instance) {
                    instance = new AdvertConfigManager();
                }
            }
        }
        return instance;
    }

    /**
     * 后台各种开关配置
     */
    public void fetchConfigInfo() {
        getCommonConfig();
//        getAdvertConfig();
        getNewAdvertConfig();
    }

    /**
     * 通用开关
     */
    private void getCommonConfig() {
        HttpData.getInstance().getConfigInfo(new AbsCallback<BaseData<ConfigInfo>>() {
            @Override
            public void onRealSuccess(BaseData<ConfigInfo> data) {
                ConfigInfo configInfo = data.getData();
                if (configInfo == null) return;
                int rate = configInfo.getHome_ad_refresh_rate();
                if (rate <= 0) {
                    rate = 4;
                }
                ShuaApplication.adRefreshRate = rate;
                int horizontalRate = configInfo.getAdvert_video_ad_num();
                if (horizontalRate <= 0) {
                    horizontalRate = 1;
                }
                ShuaApplication.horizontalRate = horizontalRate;
                try {
                    ShuaApplication.multiple_min = Integer.parseInt(configInfo.getMultiple_min());
                    ShuaApplication.multiple_max = Integer.parseInt(configInfo.getMultiple_max());
                } catch (Exception e) {

                }
                Store.getInstance().saveConfigInfo(configInfo);
            }

            @Override
            public void onFail(int errCode, String errMsg) {
            }
        });
    }

    public void saveAdvertConfig(BaseData<AdvertConfigList> data){
        if (data == null) {
            return;
        }
        AdvertConfigList configList = data.getData();
        if (configList == null) return;

        Store store = Store.getInstance();
        saveCommonConfig(configList.getCommon());
        if(configList.getReword() != null){
            configList.getReword().setIndexChest(store.getButtleIndex());
        }
        if(configList.getPatch() != null){
            PatchConfigBean patchConfig = configList.getPatch();
//                    patchConfig.setIndexCash(store.getNativeCashIndex());
            patchConfig.setIndexCommon(store.getNativeCommonIndex());
            patchConfig.setIndexReword(store.getNativeRewardIndex());
        }
        ShuaApplication.advertConfig = configList;

        if (configList.getDrawing() != null) {
            if(ShuaApplication.drawConfig == null){
                DrawConfigBean drawConfigBean = configList.getDrawing();
                drawConfigBean.setIndexDraw(Store.getInstance().getDrawIndex());
                ShuaApplication.drawConfig = drawConfigBean;
            }
            store.saveDrawConfig(configList.getDrawing().getDrawing());
        }
        if (configList.getHorizontal() != null) {
            if (ShuaApplication.horizontalConfig == null) {
                ShuaApplication.horizontalConfig = configList.getHorizontal();
            }
            store.saveHorizontalConfig(configList.getHorizontal().getHorizontal());
        }
        if (configList.getOpen() != null) {
            if (ShuaApplication.openConfig == null) {
                ShuaApplication.openConfig = configList.getOpen();
            }
            store.saveOpenConfig(configList.getOpen().getOpen());
        }
    }

    public void getNewAdvertConfig() {
        HttpData.getInstance().getNewAdvertConfig(new AbsCallback<BaseData<AdvertConfigList>>() {
            @Override
            public void onRealSuccess(BaseData<AdvertConfigList> data) {
                saveAdvertConfig(data);
            }
        });
    }

    private void saveCommonConfig(CommonConfigBean commonConfig) {
        if (commonConfig == null) {
            return;
        }
        Store store = Store.getInstance();
        if (commonConfig.getChannel_vivo_open() != null) {
            boolean isVivoOpen = commonConfig.getChannel_vivo_open().isOpen();
            ShuaApplication.isVivoOpen = isVivoOpen;
            store.setVivoOpen(isVivoOpen);
        }
        if (commonConfig.getChannel_huawei_open() != null) {
            boolean isHuaweiOpen = commonConfig.getChannel_huawei_open().isOpen();
            ShuaApplication.isHuaweiOpen = isHuaweiOpen;
            store.setHuaweiOpen(isHuaweiOpen);
        }
        if (commonConfig.getChannel_xiaomi_open() != null) {
            boolean isXiaomiOpen = commonConfig.getChannel_xiaomi_open().isOpen();
            ShuaApplication.isXiaomiOpen = isXiaomiOpen;
            store.setXiaomiOpen(isXiaomiOpen);
        }
        if (commonConfig.getAdvert_start_ad_mistake_open() != null) {
            ConfigAdvertOpen configAdvertOpen = commonConfig.getAdvert_start_ad_mistake_open();
            store.setIsAdOpen(configAdvertOpen.isOpen());
            store.setIsAdNewOpen(configAdvertOpen.isNewOpen());
            store.setIsAdOldOpen(configAdvertOpen.isOldOpen());
            store.setAdSkipTime(configAdvertOpen.getSkipTime());
        }
        if (commonConfig.getAdvert_hot_start_open() != null) {
            ConfigAdvertHot configAdvertHot = commonConfig.getAdvert_hot_start_open();
            ShuaApplication.isHotOpen = configAdvertHot.isOpen();
            ShuaApplication.hotTime = configAdvertHot.getDisTime() * 1000;
        }
        if (commonConfig.getAdvert_patch_mistake_open() != null) {
            PatchAccident advert_patch_mistake_open = commonConfig.getAdvert_patch_mistake_open();
            store.setTP_AD(advert_patch_mistake_open.getStatus() == 1);
            Store.min = advert_patch_mistake_open.getStar_rate();
            Store.max = advert_patch_mistake_open.getEnd_rate();
        }
        if (commonConfig.getAdvert_third_ad_open() != null) {
            ThirdAdOpen thirdAdOpen = commonConfig.getAdvert_third_ad_open();
            ShuaApplication.thirdAdOpen = thirdAdOpen;
            store.setTuiaHomeOpen(thirdAdOpen.getAdvert_tuia_home_fuchuang_open() == 1);
            store.setTuiaTaskOpen(thirdAdOpen.getAdvert_tuia_quanping_open() == 1);
            store.setAdmobileOpen(thirdAdOpen.getAdvert_admobile_open() == 1);
            store.setBxmOpen(thirdAdOpen.getAdvert_bianxianmao_open() == 1);
            store.setHwAdOpen(thirdAdOpen.getAdvert_huaweiqudaoguanggao_open() == 1);
        }
    }

    /**
     * 广告开关
     */
//    public void getAdvertConfig() {
//        HttpData.getInstance().getAdvertConfig(new AbsCallback<BaseData<ConfigAdvertList>>() {
//            @Override
//            public void onRealSuccess(BaseData<ConfigAdvertList> data) {
//                ConfigAdvertList configInfo = data.getData();
//                if (configInfo == null) return;
//                Store store = Store.getInstance();
//                if (configInfo.getChannel_vivo_open() != null) {
//                    ShuaApplication.isVivoOpen = configInfo.getChannel_vivo_open().isOpen();
//                }
//                if (configInfo.getChannel_huawei_open() != null) {
//                    boolean isHuaweiOpen = configInfo.getChannel_huawei_open().isOpen();
//                    ShuaApplication.isHuaweiOpen = isHuaweiOpen;
//                    store.setHuaweiOpen(isHuaweiOpen);
//                }
//                if(configInfo.getAndroid_start_ad_mistake_open() != null){
//                    ConfigAdvertOpen configAdvertOpen = configInfo.getAndroid_start_ad_mistake_open();
//                    store.setIsAdOpen(configAdvertOpen.isOpen());
//                    store.setIsAdNewOpen(configAdvertOpen.isNewOpen());
//                    store.setIsAdOldOpen(configAdvertOpen.isOldOpen());
//                    store.setAdSkipTime(configAdvertOpen.getSkipTime());
//                }
//                if(configInfo.getAndroid_hot_start_open() != null){
//                    ConfigAdvertHot configAdvertHot = configInfo.getAndroid_hot_start_open();
//                    ShuaApplication.isHotOpen = configAdvertHot.isOpen();
//                    ShuaApplication.hotTime = configAdvertHot.getDisTime() * 1000;
//                }
//                ShuaApplication.advertList = configInfo;
//            }
//
//            @Override
//            public void onFail(int errCode, String errMsg) {
//            }
//        });
//    }
}
